package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.forattitude.TypeEnumForAttitude;
import com.jilefojing.beans.enums.forcomment.StatusEnumForComment;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Comment;
import com.jilefojing.beans.pojos.RHotComment;
import com.jilefojing.beans.pojos.Statistics;
import com.jilefojing.beans.pojos.User;
import com.jilefojing.beans.pojos.enhanced.EnhancedAttitude;
import com.jilefojing.beans.pojos.enhanced.EnhancedComment;
import com.jilefojing.beans.pojos.enhanced.EnhancedEVideo;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.EnhancedUser;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.query.CommentQuery;
import com.jilefojing.beans.query.RHotCommentQuery;
import com.jilefojing.beans.vo.Order;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.ICommentDao;
import com.jilefojing.dao.IRHotCommentDao;
import com.jilefojing.dao.IUserDao;
import com.jilefojing.service.IAttitudeService;
import com.jilefojing.service.ICommentService;
import com.jilefojing.service.IEVideoService;
import com.jilefojing.service.ISourceService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.IUserService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月21日 15时40分56秒
 */
@Service
public class CommentService extends BaseService<Comment> implements ICommentService {
	@Autowired
	private ICommentDao commentDao;
	@Autowired
	private IUserService userService;
	@Autowired
	private IEVideoService eVideoService;
	@Autowired
	private IRHotCommentDao rHotCommentdDao;
	@Autowired
	private IStatisticsService statisticsService;
	@Autowired
	private IAttitudeService attitudeService;
	@Autowired
	private ISourceService sourceService;
	@Autowired
	private IUserDao userDao;

	@Override
	public Pagination<EnhancedComment> getPagination(CommentQuery commentQuery, Pagination<EnhancedComment> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<Comment> commentPagination = commentDao.getPagination(commentQuery, pagination.copy());
		pagination.setCount(commentPagination.getCount());
		if (!Detect.notEmpty(commentPagination.getItems())) {
			return pagination;
		}

		List<EnhancedComment> enhancedComments = this.conventComments2EnhancedComments(commentPagination.getItems());
		this.appendEnhancedCommons(enhancedComments, userContext);
		pagination.setItems(enhancedComments);
		return pagination;
	}

	@Override
	public List<EnhancedComment> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedComment> enhancedComments = (List<EnhancedComment>) enhancedItems;

		this.appendEnhancedUser2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedItem2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedStatistics2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedTopItem2EnhancedComments(enhancedComments, userContext);
		this.appendCountChildCommentForTop2EnhancedTopComments(enhancedComments, userContext);

		return enhancedComments;
	}

	@Override
	public EnhancedComment appendCountChildCommentForTop2EnhancedTopComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendCountChildCommentForTop2EnhancedTopComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendCountChildCommentForTop2EnhancedTopComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> topCommentIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			topCommentIds.add(_EnhancedComment.getId());
		}

		List<Map<String, Integer>> counts = commentDao.countChildCommentsForTopByTopCommentIds(topCommentIds, Arrays.asList(new StatusEnumForComment[] { StatusEnumForComment.VALID }));
		if (Detect.notEmpty(counts)) {
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				for (Map<String, Integer> _Count : counts) {
					if (_EnhancedComment.getId() == Integer.valueOf(String.valueOf(_Count.get("topCommentId")))) {
						_EnhancedComment.setCountChildCommentForTop(Integer.valueOf(String.valueOf(_Count.get("count"))));
						break;
					}
				}
			}
		}
		return enhancedComments;
	}

	@Override
	public EnhancedComment appendEnhancedItem2EnhancedComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedItem2EnhancedComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedItem2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		this.appendEnhancedEVideos2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedEAudios2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedETexts2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedComments2EnhancedComments(enhancedComments, userContext);
		// TODO 继续增加类型
		return enhancedComments;
	}

	private void appendEnhancedComments2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> commentIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			if (_EnhancedComment.getItemType() == ItemTypeEnumForSource.COMMENT) {
				commentIds.add(((EnhancedComment) _EnhancedComment.getEnhancedItem()).getId());
			}
		}
		if (Detect.notEmpty(commentIds)) {
			List<EnhancedComment> enhancedCommenteds = this.getEnhancedComments(commentIds, userContext);

			for (EnhancedComment _EnhancedComment : enhancedComments) {
				if (_EnhancedComment.getItemType() == ItemTypeEnumForSource.COMMENT) {
					for (EnhancedComment _EnhancedCommented : enhancedCommenteds) {
						if (_EnhancedCommented.getId() == ((EnhancedComment) _EnhancedComment.getEnhancedItem()).getId()) {
							_EnhancedComment.setEnhancedItem(_EnhancedCommented);
							break;
						}
					}
				}
			}
		}
	}

	private void appendEnhancedETexts2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		// TODO Auto-generated method stub
	}

	private void appendEnhancedEAudios2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		// TODO Auto-generated method stub
	}

	private void appendEnhancedEVideos2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> videoIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			if (_EnhancedComment.getItemType() == ItemTypeEnumForSource.VIDEO) {
				videoIds.add(((EnhancedEVideo) _EnhancedComment.getEnhancedItem()).getId());
			}
		}
		if (Detect.notEmpty(videoIds)) {
			List<EnhancedEVideo> enhancedEVideos = (List<EnhancedEVideo>) eVideoService.getEnhanceds(videoIds, userContext);
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				if (_EnhancedComment.getItemType() == ItemTypeEnumForSource.VIDEO) {
					for (EnhancedEVideo _EnhancedEVideo : enhancedEVideos) {
						if (_EnhancedEVideo.getId() == ((EnhancedEVideo) _EnhancedComment.getEnhancedItem()).getId()) {
							_EnhancedComment.setEnhancedItem(_EnhancedEVideo);
							break;
						}
					}
				}
			}
		}

	}

	@Override
	public EnhancedComment appendEnhancedUser2EnhancedComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedUser2EnhancedComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedUser2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> userIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			userIds.add(_EnhancedComment.getEnhancedUser().getId());
		}
		List<EnhancedUser> enhancedUsers = userService.getEnhancedUsers(userIds);
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			for (EnhancedUser _EnhancedUser : enhancedUsers) {
				if (_EnhancedComment.getEnhancedUser().getId() == _EnhancedUser.getId()) {
					_EnhancedComment.setEnhancedUser(_EnhancedUser);
					break;
				}
			}
		}
		return enhancedComments;
	}

	@Override
	public List<EnhancedComment> conventComments2EnhancedComments(List<Comment> comments) {
		List<EnhancedComment> enhancedComments = new ArrayList<EnhancedComment>();
		for (Comment _Comment : comments) {
			enhancedComments.add(new EnhancedComment(_Comment));
		}
		return enhancedComments;
	}

	@Override
	public Pagination<EnhancedComment> getPaginationForHot(ItemTypeEnumForSource itemType, long itemId, Pagination<EnhancedComment> pagination, UserContext userContext) {
		// 根据点赞的次数 以及被评论的次数排序 一次评论等于10个点赞 总数超过
		RHotCommentQuery rHotCommentQuery = new RHotCommentQuery();
		rHotCommentQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));
		rHotCommentQuery.setItemIds(Arrays.asList(new Long[] { itemId }));

		if (!Detect.notEmpty(pagination.getOrders())) {
			pagination.addOrder(new Order(RHotComment.COLUMN_SORT, Order.ORDER_DESC));
		}

		@SuppressWarnings("unchecked")
		Pagination<RHotComment> rHotCommentPagination = rHotCommentdDao.getPagination(rHotCommentQuery, pagination.copy());

		if (!Detect.notEmpty(rHotCommentPagination.getItems())) {
			pagination.setCount(rHotCommentPagination.getCount());
			return pagination;
		}
		List<Long> commentIds = new ArrayList<Long>();
		for (RHotComment _RHotComment : rHotCommentPagination.getItems()) {
			commentIds.add(_RHotComment.getCommentId());
		}
		List<EnhancedComment> enhancedComments = this.getEnhancedComments(commentIds, userContext);
		enhancedComments = this.resetHotSort(commentIds, enhancedComments);
		pagination.setItems(enhancedComments);
		pagination.setCount(rHotCommentPagination.getCount());// 重置热门总数
		return pagination;
	}

	/**
	 * 
	 * @param commentIds
	 *            排过序的id
	 * @param enhancedComments
	 *            要排序的对象
	 * @return
	 */
	private List<EnhancedComment> resetHotSort(List<Long> commentIds, List<EnhancedComment> enhancedComments) {
		List<EnhancedComment> sortEnhancedComments = new ArrayList<EnhancedComment>();
		for (long commentId : commentIds) {
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				if (commentId == _EnhancedComment.getId()) {
					sortEnhancedComments.add(_EnhancedComment);
					break;
				}
			}
		}
		return sortEnhancedComments;
	}

	@Override
	public Pagination<EnhancedComment> getPaginationForActive(ItemTypeEnumForSource itemType, long itemId, Pagination<EnhancedComment> pagination, UserContext userContext) {
		CommentQuery commentQuery = new CommentQuery();
		commentQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));
		commentQuery.setItemIds(Arrays.asList(new Long[] { itemId }));
		commentQuery.setStatuses(Arrays.asList(new StatusEnumForComment[] { StatusEnumForComment.VALID }));

		if (!Detect.notEmpty(pagination.getOrders())) {
			Order order = new Order(Comment.COLUMN_CREATE_TIME, Order.ORDER_DESC);
			pagination.addOrder(order);
		}

		return this.getPagination(commentQuery, pagination, userContext);
	}

	@Override
	public EnhancedComment getEnhancedComment(long id, UserContext userContext) {
		List<EnhancedComment> enhancedComments = this.getEnhancedComments(Arrays.asList(new Long[] { id }), userContext);
		if (!Detect.notEmpty(enhancedComments)) {
			return null;
		}
		return enhancedComments.get(0);
	}

	@Override
	public List<EnhancedComment> getEnhancedComments(List<Long> ids, UserContext userContext) {
		List<Comment> comments = commentDao.getListByIds(ids);
		if (!Detect.notEmpty(comments)) {
			return null;
		}
		List<EnhancedComment> enhancedComments = this.conventComments2EnhancedComments(comments);
		this.appendEnhancedCommons(enhancedComments, userContext);
		return enhancedComments;
	}

	@Override
	public EnhancedComment appendEnhancedStatistics2EnhancedComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedStatistics2EnhancedComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedStatistics2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> commentIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			commentIds.add(_EnhancedComment.getId());
		}
		List<EnhancedStatistics> enhancedStatisticses = statisticsService.getEnhancedStatisticses(ItemTypeEnumForSource.COMMENT, commentIds, userContext);
		if (Detect.notEmpty(enhancedStatisticses)) {
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				for (EnhancedStatistics _EnhancedStatistics : enhancedStatisticses) {
					if (_EnhancedComment.getId() == ((EnhancedComment) _EnhancedStatistics.getEnhancedItem()).getId()) {
						_EnhancedComment.setEnhancedStatistics(_EnhancedStatistics);
						break;
					}
				}
			}
		}
		return enhancedComments;

	}

	@Override
	public EnhancedComment appendEnhancedTopItem2EnhancedComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedTopItem2EnhancedComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedTopItem2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		this.appendEnhancedTopEVideos2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedTopEAudios2EnhancedComments(enhancedComments, userContext);
		this.appendEnhancedTopETexts2EnhancedComments(enhancedComments, userContext);
		// this.appendEnhancedTopComments2EnhancedComments(enhancedComments,
		// userContext); //评论暂无须成为顶部类型
		// TODO 继续增加类型
		return enhancedComments;
	}

	// private void
	// appendEnhancedTopComments2EnhancedComments(List<EnhancedComment>
	// enhancedComments, UserContext userContext) {
	// // TODO Auto-generated method stub
	//
	// }

	private void appendEnhancedTopETexts2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		// TODO Auto-generated method stub

	}

	private void appendEnhancedTopEAudios2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		// TODO Auto-generated method stub

	}

	private void appendEnhancedTopEVideos2EnhancedComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> topVideoIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			if (_EnhancedComment.getTopItemType() == ItemTypeEnumForSource.VIDEO) {
				topVideoIds.add(((EnhancedEVideo) _EnhancedComment.getEnhancedTopItem()).getId());
			}
		}
		if (Detect.notEmpty(topVideoIds)) {
			List<EnhancedEVideo> enhancedEVideos = (List<EnhancedEVideo>) eVideoService.getEnhanceds(topVideoIds, userContext);
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				if (_EnhancedComment.getTopItemType() == ItemTypeEnumForSource.VIDEO) {
					for (EnhancedEVideo _EnhancedEVideo : enhancedEVideos) {
						if (((EnhancedEVideo) _EnhancedComment.getEnhancedTopItem()).getId() == _EnhancedEVideo.getId()) {
							_EnhancedComment.setEnhancedTopItem(_EnhancedEVideo);
							break;
						}
					}
				}
			}
		}
	}

	@Override
	public EnhancedComment appendEnhancedChildCommentsForTop2EnhancedTopComment(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedChildCommentsForTop2EnhancedTopComments(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedChildCommentsForTop2EnhancedTopComments(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> topCommentIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			topCommentIds.add(_EnhancedComment.getId());
		}

		CommentQuery commentQuery = new CommentQuery();
		commentQuery.setTopCommentIds(topCommentIds);
		commentQuery.setStatuses(Arrays.asList(new StatusEnumForComment[] { StatusEnumForComment.VALID }));

		Pagination<EnhancedComment> pagination = new Pagination<EnhancedComment>(1, Integer.MAX_VALUE, false);
		Order order = new Order(Comment.COLUMN_CREATE_TIME, Order.ORDER_DESC);
		pagination.addOrder(order);

		pagination = this.getPagination(commentQuery, pagination, userContext);

		if (Detect.notEmpty(pagination.getItems())) {
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				_EnhancedComment.setEnhancedChildCommentsForTop(this.getEnhancedChildComments(_EnhancedComment, pagination.getItems()));
			}
		}
		return enhancedComments;
	}

	private List<EnhancedComment> getEnhancedChildComments(EnhancedComment enhancedComment, List<EnhancedComment> enhancedChildComments) {
		List<EnhancedComment> childs = new ArrayList<EnhancedComment>();
		for (EnhancedComment _EnhancedChildComment : enhancedChildComments) {
			if (_EnhancedChildComment.getEnhancedTopComment().getId() == enhancedComment.getId()) {
				childs.add(_EnhancedChildComment);
			}
		}
		return childs;
	}

	@Override
	public EnhancedComment appendEnhancedAttitude2EnhancedCommentForUser(EnhancedComment enhancedComment, UserContext userContext) {
		this.appendEnhancedAttitude2EnhancedCommentsForUser(Arrays.asList(new EnhancedComment[] { enhancedComment }), userContext);
		return enhancedComment;
	}

	@Override
	public List<EnhancedComment> appendEnhancedAttitude2EnhancedCommentsForUser(List<EnhancedComment> enhancedComments, UserContext userContext) {
		List<Long> videoIds = new ArrayList<Long>();
		for (EnhancedComment _EnhancedComment : enhancedComments) {
			videoIds.add(_EnhancedComment.getId());
		}

		List<EnhancedAttitude> enhancedAttitudes = attitudeService.getEnhancedAttitudesByItemsForUser(ItemTypeEnumForSource.COMMENT, videoIds, TypeEnumForAttitude.PRAISE, userContext);
		if (Detect.notEmpty(enhancedAttitudes)) {
			for (EnhancedComment _EnhancedComment : enhancedComments) {
				for (EnhancedAttitude _EnhancedAttitude : enhancedAttitudes) {
					if (_EnhancedComment.getId() == ((EnhancedComment) _EnhancedAttitude.getEnhancedItem()).getId()) {
						_EnhancedComment.setEnhancedAttitude(_EnhancedAttitude);
						break;
					}
				}
			}
		}
		return enhancedComments;
	}

	@Override
	public List<EnhancedComment> getEnhancedDialogCommentsForTop(long id, UserContext userContext) {
		EnhancedComment enhancedTopComment = null;
		EnhancedComment enhancedComment = this.getEnhancedComment(id, userContext);
		if (enhancedComment.getItemType() == ItemTypeEnumForSource.COMMENT) {// 对象是评论
			enhancedTopComment = this.getEnhancedComment(enhancedComment.getEnhancedTopComment().getId(), userContext);
		} else {
			enhancedTopComment = enhancedComment;
		}
		this.appendEnhancedChildCommentsForTop2EnhancedTopComment(enhancedTopComment, userContext);
		if (enhancedTopComment.getEnhancedChildCommentsForTop() == null) {
			enhancedTopComment.setEnhancedChildCommentsForTop(new ArrayList<EnhancedComment>());
		}
		enhancedTopComment.getEnhancedChildCommentsForTop().add(this.getEnhancedComment(enhancedTopComment.getId(), userContext));

		return enhancedTopComment.getEnhancedChildCommentsForTop();
	}

	@Override
	public Comment add(Comment comment, UserContext userContext) throws UserContextException {
		if (comment.getItemType() == null || comment.getItemId() == null) {
			throw new UserContextException(ResponseEnum.LACK_OF_NECESSARY_PARAMETERS);
		}
		if (comment.getContent() == null || comment.getContent().trim().length() < 1) {
			throw new UserContextException("[评论内容不能为空]");
		}

		if (comment.getContent().trim().length() > 200) {
			throw new UserContextException("[评论内容不能超过200字]");
		}

		Date now = new Date();
		Comment record = new Comment();
		record.setCreateTime(now);
		record.setUpdateTime(now);
		record.setUserId(userContext.getUserId());
		record.setContent(comment.getContent().trim());
		record.setItemId(comment.getItemId());
		record.setItemType(comment.getItemType());
		record.setStatus(StatusEnumForComment.VALID.getCode());

		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByCode(comment.getItemType());
		if (itemType != ItemTypeEnumForSource.COMMENT) {
			sourceService.checkSource(itemType, comment.getItemId(), userContext);
		} else {// 评论的对象特殊处理

			Comment dbComment = commentDao.getById(comment.getItemId());
			if (dbComment == null) {
				throw new UserContextException("[评论对象不存在]");
			} else if (dbComment.getUserId() == userContext.getUserId()) {
				throw new UserContextException("[不能回复自己的评论]");
			} else if (dbComment.getStatus() != StatusEnumForComment.VALID.getCode()) {
				throw new UserContextException("[评论已无效]");
			}

			Comment topComment = null;// 顶部的评论
			if (dbComment.getItemType() == ItemTypeEnumForSource.COMMENT.getCode()) {// 被评论的评论是一级以下的评论
				topComment = commentDao.getById(dbComment.getTopCommentId());
			} else {
				topComment = dbComment;
			}

			record.setTopCommentId(topComment.getId());
			record.setTopItemId(topComment.getItemId());
			record.setTopItemType(topComment.getItemType());

			// 重置评论内容
			User commentedUser = userDao.getById(dbComment.getUserId());
			if (record.getContent().startsWith("回复@" + commentedUser.getNickname() + ":")) {
				record.setContent(record.getContent().replace(("回复@" + commentedUser.getNickname() + ":"), ""));
			}
			if (!Detect.notEmpty(record.getContent().trim())) {
				throw new UserContextException("[评论内容不能为空]");
			}

		}
		comment = commentDao.saveSelective(record);
		statisticsService.updateCount(itemType, comment.getItemId(), Statistics.COLUMN_COUNT_COMMENT, true, 1, userContext);
		return comment;
	}

	@Override
	public void cancle(Comment comment, UserContext userContext) throws UserContextException {
		Comment dbComment = commentDao.getById(comment.getId());
		if (dbComment == null) {
			throw new UserContextException("[评论对象不存在]");
		} else if (dbComment.getStatus() != StatusEnumForComment.VALID.getCode()) {
			throw new UserContextException("[评论对象已被删除]");
		}
		if (dbComment.getUserId() != userContext.getUserId()) {
			throw new UserContextException(ResponseEnum.UNAUTHORIZED_ACCESS);
		}
		Comment record = new Comment();
		record.setId(comment.getId());
		record.setStatus(StatusEnumForComment.DELETE_BY_USER.getCode());
		record.setUpdateTime(new Date());

		commentDao.updateByPrimaryKeySelective(record);

		statisticsService.updateCount(ItemTypeEnumForSource.getItemTypeEnumByCode(dbComment.getItemType()), dbComment.getItemId(), Statistics.COLUMN_COUNT_COMMENT, false, 1, userContext);

	}
}
